/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.bashsoft.nero.nameparser;

/**
 *
 * @author nero
 */
public class HtmlPage {
    
    private class HtmlIndex {
        private int value;
        
        public HtmlIndex(int value) {
            this.value = value;
        }
        
        public int getValue() {
            return this.value;
        }
        
        public void setValue(int value) {
            this.value = value;
        }
    }
    
    private String html;
    private String sport;
    private HtmlIndex tournamentIndex = new HtmlIndex(0);
    private HtmlIndex subTournamentsBeginIndex = new HtmlIndex(0);
    private HtmlIndex subTournamentIndex = new HtmlIndex(0);
    private HtmlIndex subTournamentsEndIndex = new HtmlIndex(0);
    private HtmlIndex tourIndex = new HtmlIndex(0);
    private HtmlIndex clubIndex = new HtmlIndex(0);
    private HtmlIndex playerIndex = new HtmlIndex(0);    
    
    public HtmlPage(String html, String sport) {
        this.html = html;
        this.sport = sport;
    }
    
    private String getNextByPattern(String pattern, HtmlIndex index) {        
        if (index == null) index = new HtmlIndex(0);
        java.util.regex.Pattern regex = 
                java.util.regex.Pattern.compile(pattern);
        java.util.regex.Matcher matcher = regex.matcher(html);
        try {
            if (matcher.find(index.getValue())) {
                int matchIndex = matcher.start();
                if (matchIndex > 0) {                
                    index.setValue(matchIndex + 1);
                    return HtmlHelper.trim(matcher.group());
                }
            }
        }
        catch (Exception ex) {            
        }
        return null;
    }
    
    private String getNextSubTournamentByPattern(String pattern, HtmlIndex index) {        
        
        if (index == null) index = new HtmlIndex(0);
        
        if (subTournamentsBeginIndex.getValue() <= 0) {
            getNextByPattern(HtmlHelper.getSubTournamentsBlockBegin(), subTournamentsBeginIndex);
            if (subTournamentsBeginIndex.getValue() <= 0) return null;
            subTournamentsEndIndex.setValue(subTournamentsBeginIndex.getValue());
            getNextByPattern(HtmlHelper.getSubTournamentsBlockEnd(), subTournamentsEndIndex);
            if (subTournamentsEndIndex.getValue() <= 0) return null;            
        }
        
        if (index.getValue() < subTournamentsBeginIndex.getValue()) 
            index.setValue(subTournamentsBeginIndex.getValue());
        
        java.util.regex.Pattern regex = 
                java.util.regex.Pattern.compile(pattern);
        java.util.regex.Matcher matcher = regex.matcher(html);
        try {
            if (matcher.find(index.getValue())) {
                int matchIndex = matcher.start();
                if (matchIndex > 0) {               
                    if (matchIndex >= subTournamentsEndIndex.getValue())
                        return null;
                    index.setValue(matchIndex + 1);
                    return HtmlHelper.trim(matcher.group());
                }
            }
        }
        catch (Exception ex) {            
        }
        return null;
    }
    
    public String getNextTournamentUrl() {        
        String pattern = HtmlHelper.getTournamentTemplate(sport);
        return getNextByPattern(pattern, tournamentIndex);        
    }
    
    public String getNextSubTournamentUrl() {        
        String pattern = HtmlHelper.getTournamentTemplate(sport);
        return getNextSubTournamentByPattern(pattern, subTournamentIndex);        
    }
    
    public String getNextTourUrl() {        
        String pattern = HtmlHelper.getTourTemplate(sport);
        return getNextByPattern(pattern, tourIndex);        
    }
    
    public String getNextClubUrl() {        
        String pattern = HtmlHelper.getClubTemplate(sport);
        return getNextByPattern(pattern, clubIndex);
    }
    
    public String getNextPlayerUrl() {        
        String pattern = HtmlHelper.getPlayerTemplate(sport);
        return getNextByPattern(pattern, playerIndex);
    }
    
     public String getTitle() {        
        String pattern = HtmlHelper.getTitleTemplate();
        String title = getNextByPattern(pattern, null);
        if (title == null) title = "";
        return title;
    }
     
     public String getPlayerNation() {        
        String pattern = HtmlHelper.getPlayerNationTemplate();
        String nation = getNextByPattern(pattern, null);
        if (nation == null) nation = "";
        return nation;
    }
}
